﻿Shader "Unlit/UnlitShader"
{
    Properties
    {
        xPixels("xPixels", 2D) = "white" {}
        xPalette("xPalette", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" }
        // 透明的Shader
        ZWrite Off // 关闭深度写入
        Blend SrcAlpha OneMinusSrcAlpha // 混合的参数
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

         #define DECLARE_TEXTURE(Name) \
             sampler2D Name

         #define SAMPLE_TEXTURE(Name, texCoord)  tex2D(Name, texCoord)

       

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                float4 Color: COLOR0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
                float4 Color: COLOR0;
            };

            sampler2D xPixels;
            sampler2D xPalette;
            float4 xPixels_ST;

            float4x4 xMatrix;
            float4x4 xRotation;

            float xBlendWeight;

            int xPalFx_Use;
            float4 xPalFx_Add;
            float4 xPalFx_Mul;
            int xPalFx_Invert;
            float xPalFx_Color;
            float4 xPalFx_SinMath;

            int xAI_Use;
            int xAI_Invert;
            float xAI_color;
            float4 xAI_preadd;
            float4 xAI_contrast;
            float4 xAI_postadd;
            float4 xAI_paladd;
            float4 xAI_palmul;
            int xAI_number;

            int xFontColorIndex;
            int xFontTotalColors;

            float4 BaseColor(float4 inputcolor, float base)
            {
                if (base == 0.0f)
                {
                    float color = (0.299f * inputcolor.r) + (0.587f * inputcolor.g) + (0.114f * inputcolor.b);
                    return float4(color, color, color, inputcolor.a);
                }
                return float4(inputcolor.rgb * base, inputcolor.a);
            }

            float4 PalFx(float4 inputcolor)
            {
                float4 output = BaseColor(inputcolor, xPalFx_Color);

                if (xPalFx_Invert == 1) output = float4(1 - output.rgba);

                output = (output + xPalFx_Add + xPalFx_SinMath) * xPalFx_Mul;

                return float4(output.bgr, inputcolor.a);
            }

            float4 AfterImage(float4 inputcolor)
            {
                float4 output = BaseColor(inputcolor, xAI_color);

                if (xAI_Invert == 1) output = float4(1 - output.rgba);

                output += xAI_preadd;
                output *= xAI_contrast;
                output += xAI_postadd;

                [unroll(25)]
                for (int i = 0; i <= xAI_number; ++i) output = (output + xAI_paladd) * xAI_palmul;

                return float4(output.bgr, inputcolor.a);
            }

            v2f vert (appdata v)
            {
                v2f o;
                //o.vertex = UnityObjectToClipPos(v.vertex);
                o.vertex= mul(mul(v.vertex, xMatrix), xRotation);
                //o.uv = TRANSFORM_TEX(v.uv, xPixels);
                o.uv = v.uv;
                o.Color = v.Color;
                //UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                //// sample the texture
                //fixed4 col = tex2D(xPixels, i.uv);
                //// apply fog
                //UNITY_APPLY_FOG(i.fogCoord, col);
                //return col;


                 float color_index = SAMPLE_TEXTURE(xPixels, i.uv).r;
                 if (color_index == 0.0f) return float4(0, 0, 0, 0);

                 float4 output_color = SAMPLE_TEXTURE(xPalette, color_index);

                  if (xPalFx_Use == 1) output_color = PalFx(output_color);
                  if (xAI_Use == 1) output_color = AfterImage(output_color);

                   output_color *= i.Color;

                   return float4(output_color.bgr, xBlendWeight);
            }

           

            ENDCG
        }

       
    }
}
